﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using IdentityServer4.Extensions;
using IdentityServer4.Models;
using IdentityServer4.Services;
using IdentityServerAspNetIdentity.Models;
using Microsoft.AspNetCore.Identity;

namespace IdentityServerAspNetIdentity
{
    public class AbpProfileService : IProfileService
    {
        protected UserManager<ApplicationUser> _userManager;

        public AbpProfileService(UserManager<ApplicationUser> userManager)
        {
            _userManager = userManager;
        }

        public async Task GetProfileDataAsync(ProfileDataRequestContext context)
        {
            try
            {
                //                var user = await _userManager.GetUserAsync(context.Subject);

                //                var claims = new List<Claim>
                //                {
                //                    new Claim("unique_name", user.Email),
                //                };

                var nameClaim = context.Subject.Claims.Where(c => c.Type == "name").FirstOrDefault();
                var emailClaim = context.Subject.Claims.Where(c => c.Type == "email").FirstOrDefault();
                var claims = new List<Claim>
                {
                    new Claim("name", nameClaim.Value),
                    new Claim("email_address", emailClaim.Value),
                };
//                var claims = context.Subject.Claims.Where(c => new List<string>(){"name","email" }.Contains(c.Type) ).ToList();
                context.IssuedClaims.AddRange(claims);
                await Task.FromResult(0);
            }
            catch (Exception ex)
            {
                //log your error
            }
        }

        public async Task IsActiveAsync(IsActiveContext context)
        {
            context.IsActive = true;
            await Task.FromResult(0);
        }
    }

    public class IdentityProfileService : IProfileService
    {

        private readonly IUserClaimsPrincipalFactory<ApplicationUser> _claimsFactory;
        private readonly UserManager<ApplicationUser> _userManager;

        public IdentityProfileService(IUserClaimsPrincipalFactory<ApplicationUser> claimsFactory, UserManager<ApplicationUser> userManager)
        {
            _claimsFactory = claimsFactory;
            _userManager = userManager;
        }

        public async Task GetProfileDataAsync(ProfileDataRequestContext context)
        {
            var sub = context.Subject.GetSubjectId();
            var user = await _userManager.FindByIdAsync(sub);
            if (user == null)
            {
                throw new ArgumentException("");
            }

            var principal = await _claimsFactory.CreateAsync(user);
            var claims = principal.Claims.ToList();

            //Add more claims like this
            //claims.Add(new System.Security.Claims.Claim("MyProfileID", user.Id));

            context.IssuedClaims = claims;
        }

        public async Task IsActiveAsync(IsActiveContext context)
        {
            var sub = context.Subject.GetSubjectId();
            var user = await _userManager.FindByIdAsync(sub);
            context.IsActive = user != null;
        }
    }
}
